package com.cusprovider.service;

import com.cusprovider.dao.StaffDao;
import com.pojo.Staff;
import com.service.StaffService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StaffServiceImpl implements StaffService {

    @Resource
    private StaffDao staffDao ;

    @Override
    public Staff getStaffByLoginCode(String loginCode) {
        return staffDao.getStaffByLoginCode(loginCode);
    }

    @Override
    public List<Staff> getAllStaffs() {
        return staffDao.getAllStaffs() ;
    }

    @Override
    public Staff getStaffById(int uid) {
        return staffDao.getStaffById(uid);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int addStaff(Staff staff, int[] roleIds) {
        int success = 0 ;

        //先将用户加入用户表中
        staffDao.addStaff(staff) ;
        System.out.println(staff.getId());

        //循环将用户选择的角色加入中间表
        for(int rid : roleIds) {
            staffDao.addStaffRole(staff.getId(), rid) ;
        }


        success = 1 ;

        return success ;
    }

    @Override
    public int staffEdit(Staff staff) {
        return staffDao.StaffEdit(staff);
    }

    //回显
    @Override
    public Staff getId (int id){
        return staffDao.getId(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean staffDelete(int id) {
        int v=staffDao.staffDelete(id);
        if(v==1){
            return true;
        }else {
            return false;
        }
    }
}
